using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Web.Caching;
using System.Collections.Generic;

namespace MTRC.ODMS.Web.SOM
{
    public partial class NewsBar : System.Web.UI.Page//BasePage_SOM
    {
        protected Model.Security.UserInfo ODMUser
        {
            get
            {
                return BasePage.CurUser;
            }
            set
            {
                Session[BLL.COM.Session.COM_Current_User] = value;
            }
        }
        protected string LangCode
        {
            get
            {
                return BasePage.CurLangCode;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            Response.Charset = "UTF-8";
            //Response.Cache.SetExpires(DateTime.Now.AddSeconds(4));
            //update by benson 2015.7.9 begin
            //this.BindNewsBarData();
            //Response.Write(GetStringByControl(this.PnlNews));

            string newstr = this.BindNewsBarData();
            Session["newsStr"] = newstr;//add by gordon 20160128 ISU32
            Response.Write(newstr);
            //update by benson 2015.7.9 end

            //Response.Write(GetStringByControl(this.NewsBarMsg));
            //Response.Write(DateTime.Now.ToString());
            Response.End();
            //litNewsBar.Text = newstr;

            //Comment by Andy 20090118
            //if (!IsPostBack)
            //{
            //    int interval = 60000;
            //    try{
            //        object objRefreshMinutes = DAL.CommonData.GetNumVal("SOM_NewsBar_RefreshMinutes");
            //        if (objRefreshMinutes != null)
            //        {
            //            interval = Convert.ToInt32(objRefreshMinutes);
            //            interval = interval * 60 * 1000;
            //        }

            //        this.timerNewsBar.Interval = interval;
            //    }catch{
            //        this.timerNewsBar.Interval = 60000;
            //    }
            //    this.timerNewsBar.Enabled = true;
            //}
        }

        //Add by Andy, get the html genreating from control
        public string GetStringByControl(System.Web.UI.Control c)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            System.IO.StringWriter writer = new System.IO.StringWriter(sb);
            System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(writer);
            c.RenderControl(htw);
            return sb.ToString();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            //this.BindNewsBarData();
        }
        //update by benson 2015.7.9 begin
        //private void BindNewsBarData()
        private string BindNewsBarData()
        //update by benson 2015.7.9 end
        {
            //NewsBarMsg.Controls.Clear();
            // comment by Kingkong Chan 20140731 for Code Merge (Changeset: 25114, 25161)
            //if (!DAL.Security.User.CanFunctionItemAccess("M14_F1", this.ODMUser.UserProfileID, "M14"))
            //{
            //    return;
            //}
            // add by Kingkong Chan 20140731 for Code Merge (Changeset: 25114, 25161)
            if (this.ODMUser == null || !DAL.Security.User.CanFunctionItemAccess("M14_F1", this.ODMUser.UserProfileID, "M14"))
            {
                return "";
            }

            // comment by Kingkong Chan 20140731 for Code Merge (Changeset: 25114, 25161)
            //DataTable dt = new DataTable();
            //if (Convert.ToBoolean(Common.GetAppSetting("EnableCache")))
            //{
            //    string cacheKey = "LastUpdTime";
            //    if (APJ.Util.DataAccessHelper.CacheHelper.GetCache(cacheKey) == null)
            //    {
            //        dt = DAL.SOM.CodeSetup.ReminderMsgDa.GetNewsBarMsg(this.LangCode, this.ODMUser);

            //        APJ.Util.DataAccessHelper.CacheHelper.AddCache(cacheKey, dt, APJ.Util.DataAccessHelper.CacheHelper.GetSqlCacheDependency("SOM_ReminderMsg"));
            //    }
            //    else
            //    {
            //        dt = (DataTable)APJ.Util.DataAccessHelper.CacheHelper.GetCache(cacheKey);
            //    }
            //}
            //else
            //{
            //    dt = DAL.SOM.CodeSetup.ReminderMsgDa.GetNewsBarMsg(this.LangCode, this.ODMUser);
            //}
            // add by Kingkong Chan 20140731 for Code Merge (Changeset: 25114, 25161)
            //Updated by kernel 2012-05-16 for no need use cache for SOM_ReminderMsg as updated aways
            DataTable dt = DAL.SOM.CodeSetup.ReminderMsgDa.GetNewsBarMsg(this.LangCode, this.ODMUser);
            string list = string.Empty;
            IList<string> listRemindmsg = new List<string>(); //Add by jack 20151126 U286.3 use json save result
            if (dt.Rows.Count > 0)
            {

                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    //Literal lit = new Literal();

                    string ReminderMsgID = dt.Rows[i].ItemArray[0].ToString();//get the message's id

                    //Add by LiangZehong on 2008-12-08 Begin...
                    int EventTypeID = Convert.ToInt32(dt.Rows[i].ItemArray[2].ToString());
                    string imgSrc = string.Empty;
                    switch (EventTypeID)
                    {
                        case 1: //admission registry
                            imgSrc = "~/Images/SOM/EventType_AR.gif";
                            break;
                        case 2: //asset loan
                            imgSrc = "~/Images/SOM/EventType_AL.gif";
                            break;
                        case 3: //fire link
                            imgSrc = "~/Images/SOM/EventType_FL.gif";
                            break;
                        case 4: //general book on for work
                            imgSrc = "~/Images/SOM/EventType_GE.gif";
                            break;
                        case 5: //Duty SC book On 
                            imgSrc = "~/Images/SOM/EventType_DSCB.gif";
                            break;
                        default:
                            imgSrc = string.Empty;
                            break;
                    }
                    if (!string.IsNullOrEmpty(imgSrc))
                    {
                        imgSrc = this.Page.ResolveUrl(imgSrc);
                        imgSrc = string.Format("<img src='{0}' border='0' />&nbsp;", imgSrc);
                    }
                    //End...
                    string url = this.Page.ResolveUrl("~/SOM/Popup/PopupNewsBarMsgForm.aspx");
                    url = "javascript:window.open('" + url + "?MessageID=" + ReminderMsgID + "','newwindow','Height=250px,Width=450px,center=yes,resizable=no,scroll=no,status=no,resizable=no'); return false;";
                    
                    //Update by jack 20151126 U286.3 use json save result
                    //if (string.IsNullOrEmpty(list))
                    //{
                    //    list = string.Format("<a href='#' title='" + HttpUtility.HtmlEncode(dt.Rows[i].ItemArray[1].ToString()) + "' onclick=\"{2}\">{0}<span style=\"vertical-align:top;color:red\">{1}</span></a>", imgSrc, dt.Rows[i].ItemArray[1].ToString(), url);
                    //}
                    //else
                    //{
                    //    list = list + "||" + string.Format("<a href='#' title='" + HttpUtility.HtmlEncode(dt.Rows[i].ItemArray[1].ToString()) + "' onclick=\"{2}\">{0}<span style=\"vertical-align:top;color:red\">{1}</span></a>", imgSrc, dt.Rows[i].ItemArray[1].ToString(), url);
                    //}

                    //listRemindmsg.Add(string.Format("<a href='#' title='" + HttpUtility.HtmlEncode(dt.Rows[i].ItemArray[1].ToString()) + "' onclick=\"{2}\">{0}<span style=\"vertical-align:top;color:red\">{1}</span></a>", imgSrc, dt.Rows[i].ItemArray[1].ToString(), url));
                    listRemindmsg.Add("<a href='#' title='" + HttpUtility.HtmlEncode(dt.Rows[i].ItemArray[1].ToString()) + "' onclick=\""+url+"\">"+imgSrc+"<span style=\"vertical-align:top;color:red\">"+dt.Rows[i].ItemArray[1].ToString()+"</span></a>");
                    //End update by jack 20151126 U286.3 use json save result

                    //NewsBarMsg.Controls.Add(lit);//put the new label into the div
                }

            }

            //Update by jack 20151126 U286.3 use json save result
            System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
            list = json.Serialize(listRemindmsg);
            //End update by jack 20151126 U286.3 use json save result

            return list;
        }

        protected void timerNewsBar_Tick(object sender, EventArgs e)
        {
            this.BindNewsBarData();
        }

      
    }
}
